package com.zzh.lib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Region;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ProgressBar;

import com.zzh.lib.dialog.R;
import com.zzh.lib.xpopup.util.XPopupUtils;

/**
 * @Date: 2021/6/8 10:37
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 带按钮的水平进度条
 */
public class HorizontalDownloadProgressBar extends ProgressBar {

    public static final int DOWNLOADING = 0x001;//下载
    public static final int PAUSE = 0x002;//下载暂停
    public static final int FINISH = 0x003;//下载完成
    public static final int START = 0x004;//下载开始
    private int mState = START;//默认状态时开始
    private int mProgressTextColor = Color.parseColor("#4599E9");//进度条文字默认颜色
    private int mProgressBarColor = Color.parseColor("#D4E9FA");//进度条默认背景颜色
    private int mProgressTextSize = 18;//进度条中字体的大小
    private int mProgressMinHeight = 20;//进度条最小高度
    //进度条最小宽度
    private Paint mPaint;
    private int mProgressMinWidth = 30;
    private Path mPath;

    private String downloading = "下载中";
    private String pause = "暂停";
    private String finish = "下载完成";
    private String error = "下载失败";

    public void setProgressState(int state) {
        mState = state;
        postInvalidate();
    }


    public int getProgressState() {
        return mState;
    }


    public HorizontalDownloadProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs, 0);//此处要这样搞
        obtainStyledAttributes(context, attrs);
        initProgressBar();
    }

    //初始化xml中设置的自定义属性
    private void obtainStyledAttributes(Context context, AttributeSet attrs) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HorizontalDownloadProgressBar);
        mProgressTextColor = ta.getColor(R.styleable.HorizontalDownloadProgressBar_zh_progress_text_color, mProgressTextColor);
        mProgressTextSize = (int) ta.getDimension(R.styleable.HorizontalDownloadProgressBar_zh_progress_text_size, mProgressTextSize);
        mProgressBarColor = ta.getColor(R.styleable.HorizontalDownloadProgressBar_zh_progress_bar_color, mProgressBarColor);
        mProgressMinHeight = (int) ta.getDimension(R.styleable.HorizontalDownloadProgressBar_zh_progress_min_height, mProgressMinHeight);
        mProgressMinWidth = (int) ta.getDimension(R.styleable.HorizontalDownloadProgressBar_zh_progress_min_width, mProgressMinWidth);
        if (ta.hasValue(R.styleable.HorizontalDownloadProgressBar_zh_progress_downloading)) {
            downloading = ta.getString(R.styleable.HorizontalDownloadProgressBar_zh_progress_downloading);
        }
        if (ta.hasValue(R.styleable.HorizontalDownloadProgressBar_zh_progress_pause)) {
            pause = ta.getString(R.styleable.HorizontalDownloadProgressBar_zh_progress_pause);
        }
        if (ta.hasValue(R.styleable.HorizontalDownloadProgressBar_zh_progress_error)) {
            error = ta.getString(R.styleable.HorizontalDownloadProgressBar_zh_progress_error);
        }
        if (ta.hasValue(R.styleable.HorizontalDownloadProgressBar_zh_progress_finish)) {
            finish = ta.getString(R.styleable.HorizontalDownloadProgressBar_zh_progress_finish);
        }
        ta.recycle();
    }

    private void initProgressBar() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPath = new Path();
        mPaint.setTextSize(XPopupUtils.sp2px(getContext(), mProgressTextSize));
        mPaint.setStrokeWidth(XPopupUtils.dp2px(getContext(), 1));
        setClickable(true);//设置为可点击
    }

    //此处主要测量文字的高度和ProgressBar的高度之间的最大值
    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //int heightSize = measureHeight(heightMeasureSpec);//拿到高度
        //比较一下文字的高度和height的大小，谁大用谁的
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), heightMeasureSpec);
    }

    //分别绘制四种状态
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        switch (mState) {
            case DOWNLOADING:
                drawProgressOnDownload(canvas);
                break;
            case PAUSE:
                drawProgressOnPause(canvas);
                break;
            case FINISH:
                drawProgressOnFinished(canvas);
                break;
            case START:
                drawProgressOnStart(canvas);
                break;
            default:
                break;
        }
    }

    //下载的时候
    private void drawProgressOnDownload(Canvas canvas) {
        drawProgressRectBackground(canvas);
        drawProgressRect(canvas);
        String progressValue = TextUtils.concat(String.valueOf(getProgress()), "%").toString();
        drawProgressText(canvas, progressValue);
        if (getProgress() == getMax()) {
            postInvalidateDelayed(1000);
            mState = FINISH;
        }
    }

    //暂停的时候
    private void drawProgressOnPause(Canvas canvas) {
        drawProgressRectBackground(canvas);
        drawProgressRect(canvas);
        drawProgressText(canvas, pause);
    }

    //下载完成的时候
    private void drawProgressOnFinished(Canvas canvas) {
        drawProgressRectBackground(canvas);
        drawProgressText(canvas, finish);
        setEnabled(false);
    }

    //还没有开始下载的时候
    private void drawProgressOnStart(Canvas canvas) {
        drawProgressRectBackground(canvas);
        drawProgressText(canvas, downloading);
    }


    //  绘制矩形的下载进度
    private void drawProgressRect(Canvas canvas) {
        int width = getWidth() - getPaddingRight() - getPaddingLeft();//考虑左右边距
        int height = getHeight() - getPaddingBottom() - getPaddingTop();//考虑上下边距
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mProgressBarColor);
        float radio = getProgress() * 1.0f / getMax();
        int progress = (int) (width * radio);
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());
        mPath.reset();
        //绘制一个路径
        mPath.moveTo(height / 2.0f, height);
        mPath.arcTo(new RectF(0, 0, height * 1.0f, height * 1.0f), 90, 180);
        mPath.lineTo(width - height / 2.0f, 0);
        mPath.arcTo(new RectF(width - height, 0, width, height), -90, 180);
        mPath.lineTo(width - height / 2.0f, height);
        mPath.lineTo(height / 2.0f, height);
        mPath.close();
        //绘制进度
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(mProgressBarColor);
        canvas.clipPath(mPath);
        mPath.reset();
        mPath.addRect(new RectF(0, 0, progress, height), Path.Direction.CCW);
        canvas.clipPath(mPath, Region.Op.INTERSECT);
        canvas.drawColor(mProgressBarColor);
        canvas.restore();
    }


    //  绘制背景矩形
    private void drawProgressRectBackground(Canvas canvas) {
        int width = getWidth() - getPaddingRight() - getPaddingLeft();
        int height = getHeight() - getPaddingBottom() - getPaddingTop();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mProgressBarColor);
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());
        //绘制一个路径
        mPath.reset();
        float pw = mPaint.getStrokeWidth();//画笔的宽度
        mPath.moveTo(height / 2.0f - pw / 2, height - pw / 2);//从这个点开始
        mPath.arcTo(new RectF(pw / 2, pw / 2, height * 1.0f - pw / 2, height * 1.0f - pw / 2), 90, 180);//添加一个左边弧形
        mPath.lineTo(width - height / 2.0f - pw / 2, pw / 2);//连接这个点
        mPath.arcTo(new RectF(width - height + pw / 2, pw / 2, width - pw / 2, height - pw / 2), -90, 180);//添加一个右边弧形
        mPath.lineTo(width - height / 2.0f - pw / 2, height - pw / 2);//连接这个点
        mPath.lineTo(height / 2.0f - pw / 2, height - pw / 2);//连接这个点
        mPath.close();//关闭
        canvas.drawPath(mPath, mPaint);
        canvas.restore();
    }


    //  绘制进度中的文字
    private void drawProgressText(Canvas canvas, String text) {
        int width = getWidth() - getPaddingRight() - getPaddingLeft();
        int height = getHeight() - getPaddingBottom() - getPaddingTop();
        //绘制文字
        mPaint.setStyle(Paint.Style.FILL);
        canvas.save();
        canvas.translate(getPaddingLeft(), getPaddingTop());
        mPaint.setColor(mProgressTextColor);
        int textWidth = (int) mPaint.measureText(text);
        int textHeight = (int) (mPaint.descent() + mPaint.ascent());
        canvas.drawText(text, width / 2.0f - textWidth / 2.0f, height / 2.0f - textHeight / 2.0f, mPaint);
        canvas.restore();
    }

}

